*&---------------------------------------------------------------------*
*& 包含               YBC_SWS_USER_MANAGE_F01
*&---------------------------------------------------------------------*
FORM FORM_INIT .
  DATA G_FUNCTXT TYPE SMP_DYNTXT.
  G_FUNCTXT-ICON_ID       = ICON_ADDRESS.
  G_FUNCTXT-QUICKINFO     = TEXT-F01.
  G_FUNCTXT-ICON_TEXT     = TEXT-F01.
  SSCRFIELDS-FUNCTXT_01 = G_FUNCTXT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form form_download_tmpl
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_DOWNLOAD_TMPL .
  IF SSCRFIELDS-UCOMM EQ 'FC01'.
    DATA:
      LV_TITLE    TYPE STRING,
      LV_DIR      TYPE STRING,
      LV_RC       TYPE SY-SUBRC,
      LV_TEMPLATE TYPE RLGRAP-FILENAME,
      ST_TMPL_KEY TYPE WWWDATATAB.

    LV_TITLE = TEXT-003.
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE
      EXPORTING
        WINDOW_TITLE         = LV_TITLE
      CHANGING
        SELECTED_FOLDER      = LV_DIR
      EXCEPTIONS
        CNTL_ERROR           = 1
        ERROR_NO_GUI         = 2
        NOT_SUPPORTED_BY_GUI = 3
        OTHERS               = 4.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CONCATENATE LV_DIR '\' GC_TEMPLATE_NM '_' SY-DATUM '_' SY-UZEIT '.xlsx' INTO LV_TEMPLATE.
    ST_TMPL_KEY-RELID = 'MI'.
    ST_TMPL_KEY-OBJID = 'YBC_SWS_USER_IMPORT'.
    ST_TMPL_KEY-TEXT = SY-TITLE.

    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        KEY         = ST_TMPL_KEY
        DESTINATION = LV_TEMPLATE
      IMPORTING
        RC          = LV_RC.
    IF LV_RC <> 0.
      MESSAGE TEXT-004 TYPE 'E'.
    ELSE.
      MESSAGE TEXT-005 TYPE 'S'.
    ENDIF.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_SCREEN_SET
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_SCREEN_SET .
  LOOP AT SCREEN.
    IF P_IMPORT IS NOT INITIAL.
      IF SCREEN-GROUP1 = 'M02'.
        SCREEN-ACTIVE = '0'.
      ENDIF.
    ELSE.
      IF SCREEN-GROUP1 = 'M01'.
        SCREEN-ACTIVE = '0'.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_GET_DATA .
  SELECT *
    INTO TABLE GT_RESULT
    FROM YBC_SWS_USER
   WHERE EXTUSID IN S_EXTID
     AND SAPID IN S_SAPID
     AND USERTY IN S_USERTY
     AND EMAIL IN S_EMAIL
     AND TEL IN S_TEL
     AND WECHAT IN S_WECHAT
     AND NAME1 IN S_NAME1
     AND NAME2 IN S_NAME2
     AND NAME3 IN S_NAME3.
  IF SY-SUBRC <> 0.
    MESSAGE S002 DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_ALV_DISPLAY .
  DATA:
    LR_DISPLAY   TYPE REF TO CL_SALV_DISPLAY_SETTINGS,
    LR_FUNC_LIST TYPE REF TO CL_SALV_FUNCTIONS_LIST,
    LR_EVENTS    TYPE REF TO CL_SALV_EVENTS_TABLE,
    LR_SELECTS   TYPE REF TO CL_SALV_SELECTIONS,
    LR_COLUMNS   TYPE REF TO CL_SALV_COLUMNS_TABLE,
    LR_COLUMN    TYPE REF TO CL_SALV_COLUMN_TABLE,
    LR_LAYOUT    TYPE REF TO CL_SALV_LAYOUT,
    ST_KEY       TYPE SALV_S_LAYOUT_KEY.

  FIELD-SYMBOLS: <FT_TAB>.

  IF P_IMPORT IS NOT INITIAL.
    ASSIGN GT_USER_IMPORT TO <FT_TAB>.
  ELSE.
    ASSIGN GT_RESULT TO <FT_TAB>.
  ENDIF.

  TRY.
      CL_SALV_TABLE=>FACTORY(
        IMPORTING
          R_SALV_TABLE = GR_TABLE
        CHANGING
          T_TABLE      = <FT_TAB>
      ).
    CATCH CX_SALV_MSG.
  ENDTRY.

  "设置列Lable
  LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
  LR_COLUMNS->SET_OPTIMIZE( 'X' ).
  LR_COLUMNS->SET_KEY_FIXATION( 'X' ).

  "显示设置
  LR_DISPLAY = GR_TABLE->GET_DISPLAY_SETTINGS( ).
  LR_DISPLAY->SET_STRIPED_PATTERN( 'X' ). "设置颜色间隔显示

  "设置选择模式
  LR_SELECTS = GR_TABLE->GET_SELECTIONS( ).
  LR_SELECTS->SET_SELECTION_MODE( CL_SALV_SELECTIONS=>IF_SALV_C_SELECTION_MODE~CELL ).

  "STAUTS ALV 标准功能
*  LR_FUNC_LIST = GR_TABLE->GET_FUNCTIONS( ).
*  LR_FUNC_LIST->SET_ALL( 'X' ).

  "打开Layout保存功能
  LR_LAYOUT = GR_TABLE->GET_LAYOUT( ).
  ST_KEY-REPORT = SY-REPID.
  LR_LAYOUT->SET_KEY( ST_KEY ).
  LR_LAYOUT->SET_DEFAULT( ABAP_TRUE ).
  LR_LAYOUT->SET_SAVE_RESTRICTION( IF_SALV_C_LAYOUT=>RESTRICT_NONE ).

  "设置ALV自定义按钮功能
  IF P_IMPORT IS INITIAL.
    GR_TABLE->SET_SCREEN_STATUS(
      PFSTATUS      = '1000'
      REPORT        = SY-REPID
      SET_FUNCTIONS = GR_TABLE->C_FUNCTIONS_ALL ).
  ELSE.
    GR_TABLE->SET_SCREEN_STATUS(
      PFSTATUS      = '1010'
      REPORT        = SY-REPID
      SET_FUNCTIONS = GR_TABLE->C_FUNCTIONS_ALL ).
  ENDIF.


  "用户自定义按钮事件接受
  LR_EVENTS = GR_TABLE->GET_EVENT( ).
  CREATE OBJECT GR_EVENTS_HANDLER. "type ref to lcl_events.
  SET HANDLER GR_EVENTS_HANDLER->ON_LINK_CLICK   FOR LR_EVENTS.
  SET HANDLER GR_EVENTS_HANDLER->ON_USER_COMMAND FOR LR_EVENTS.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'MANDT' ).
      LR_COLUMN->SET_VISIBLE( ABAP_FALSE ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'ICON' ).
      LR_COLUMN->SET_SHORT_TEXT( TEXT-T03 ).
      LR_COLUMN->SET_MEDIUM_TEXT( TEXT-T03 ).
      LR_COLUMN->SET_LONG_TEXT( TEXT-T03 ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  GR_TABLE->DISPLAY( ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_ENPASSWORD
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GC_INITPWD
*&      <-- LS_RESULT_PASSWORD
*&---------------------------------------------------------------------*
FORM FORM_ENPASSWORD  USING    IV_PASSWORD
                      CHANGING CV_PASSWORD.
  "密码转换
  DATA:
    LO_ENCRYPTOR        TYPE REF TO CL_HARD_WIRED_ENCRYPTOR,
    LO_CX_ENCRYPT_ERROR TYPE REF TO CX_ENCRYPT_ERROR,
    LV_IN_STRING        TYPE STRING,
    LV_OUT_STRING       TYPE STRING,
    LV_ERROR_MSG        TYPE STRING.

  CREATE OBJECT LO_ENCRYPTOR.

  LV_IN_STRING = IV_PASSWORD.
  TRY.
      CALL METHOD LO_ENCRYPTOR->ENCRYPT_STRING2STRING
        EXPORTING
          THE_STRING = LV_IN_STRING
        RECEIVING
          RESULT     = LV_OUT_STRING.
    CATCH CX_ENCRYPT_ERROR INTO LO_CX_ENCRYPT_ERROR.
      CALL METHOD LO_CX_ENCRYPT_ERROR->IF_MESSAGE~GET_TEXT
        RECEIVING
          RESULT = LV_ERROR_MSG.
      MESSAGE LV_ERROR_MSG TYPE 'E'.
  ENDTRY.

  CV_PASSWORD = LV_OUT_STRING."加密后密码
ENDFORM.
*&---------------------------------------------------------------------*
*& Form form_get_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_GET_FILE .
  DATA:
    LT_FILE_TABLE TYPE FILETABLE,
    LS_FILE_TABLE TYPE LINE OF FILETABLE,
    LV_RC         TYPE I.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = 'Select Source Excel File'
*     default_extension       = ''
*     default_filename        =
      FILE_FILTER             = 'Excel Files (*.xlsx)|*.xlsx'
*     with_encoding           =
*     initial_directory       =
*     multiselection          =
    CHANGING
      FILE_TABLE              = LT_FILE_TABLE
      RC                      = LV_RC
*     user_action             =
*     file_encoding           =
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      NOT_SUPPORTED_BY_GUI    = 4
      OTHERS                  = 5.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  READ TABLE LT_FILE_TABLE INTO LS_FILE_TABLE INDEX 1.
  IF SY-SUBRC = 0.
    P_FILE = LS_FILE_TABLE-FILENAME.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_UPLOAD_DATA .
  IF P_FILE IS INITIAL.
    MESSAGE S049 DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  DATA:
    LV_FILENAME TYPE RLGRAP-FILENAME,
    LT_EXCEL    TYPE STANDARD TABLE OF ALSMEX_TABLINE,
    LS_EXCEL    TYPE ALSMEX_TABLINE.

  FIELD-SYMBOLS: <FS_FIELD> TYPE ANY.

  LV_FILENAME = P_FILE.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                = LV_FILENAME
      I_BEGIN_COL             = 1      "第一列开始
      I_BEGIN_ROW             = 2      "第二行开始
      I_END_COL               = 100    "最多100列
      I_END_ROW               = 9999                  "最多9999行
    TABLES
      INTERN                  = LT_EXCEL
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 2
      OTHERS                  = 3.
  IF SY-SUBRC <> 0.
    MESSAGE 'Excel file read error，please check(*.xls/*.xlsx)' TYPE 'E'.
  ENDIF.

  LOOP AT LT_EXCEL INTO LS_EXCEL.
    LS_EXCEL-COL = LS_EXCEL-COL + 1. "跳过ICON列

    ASSIGN COMPONENT LS_EXCEL-COL OF STRUCTURE GS_USER_IMPORT TO <FS_FIELD>. "对字段赋值，内表字段顺序要和Excel完全一致
    <FS_FIELD> = LS_EXCEL-VALUE.

    AT END OF ROW.
      TRANSLATE:
        GS_USER_IMPORT-EMAIL TO UPPER CASE,
        GS_USER_IMPORT-EXTUSID TO UPPER CASE.
      GS_USER_IMPORT-ICON = ICON_GREEN_LIGHT.

      PERFORM FORM_CHECK_USER CHANGING GS_USER_IMPORT.
      APPEND GS_USER_IMPORT TO GT_USER_IMPORT.
      CLEAR GS_USER_IMPORT.
    ENDAT.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FORM_CHECK_USER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- GS_USER_IMPORT
*&---------------------------------------------------------------------*
FORM FORM_CHECK_USER  CHANGING CS_USER_IMPORT TYPE TY_USER_IMPORT.
  SELECT COUNT(*)
    FROM YBC_SWS_USER
   WHERE EXTUSID = CS_USER_IMPORT-EXTUSID.
  IF SY-SUBRC = 0.
    CS_USER_IMPORT-ICON = ICON_RED_LIGHT.
    CS_USER_IMPORT-MESSAGE = TEXT-006.
  ENDIF.

  SELECT COUNT(*)
   FROM YBC_SWS_USER
  WHERE SAPID = CS_USER_IMPORT-EXTUSID.
  IF SY-SUBRC = 0.
    CS_USER_IMPORT-ICON = ICON_RED_LIGHT.
    CS_USER_IMPORT-MESSAGE = TEXT-006.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.



  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.

  CASE SAVE_OK.
    WHEN 'BTN_OK'.
      PERFORM FORM_AGENT_OK.

    WHEN 'BTN_CANL'.
      LEAVE TO SCREEN 0.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
  SET PF-STATUS '9000'.
  SET TITLEBAR '9000'.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  SYSTEM_EXIT_COMMAND  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE SYSTEM_EXIT_COMMAND INPUT.
  LEAVE TO SCREEN 0.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form FRM_AGENT_OK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FORM_AGENT_OK .
  DATA:
    LS_SOLID    TYPE LVC_S_STBL,
    LR_COLUMNS  TYPE REF TO CL_SALV_COLUMNS_TABLE,
    LT_APPROVER TYPE STANDARD TABLE OF YBCS_SWS_APPROVER.

  IF YBC_SWS_USER-AG_BEGDA < SY-DATUM OR YBC_SWS_USER-AG_BEGDA > YBC_SWS_USER-AG_ENDDA.
    MESSAGE E057(YBC_SWS).

  ELSE.

    GS_RESULT-AG_BEGDA = YBC_SWS_USER-AG_BEGDA.
    GS_RESULT-AG_ENDDA = YBC_SWS_USER-AG_ENDDA.
    GS_RESULT-AG_USERID = YBC_SWS_USER-AG_USERID.
    GS_RESULT-AG_TODO_COPY = YBC_SWS_USER-AG_TODO_COPY.

    GS_RESULT-LAUSER = SY-UNAME.
    GS_RESULT-LADATE = SY-DATUM.
    GS_RESULT-LATIME = SY-UZEIT.

    MODIFY YBC_SWS_USER FROM GS_RESULT.

    COMMIT WORK AND WAIT.

    "复制待办到代理人下
    IF GS_RESULT-AG_TODO_COPY IS NOT INITIAL.
      SELECT A~REQUESTID
        FROM YBC_SWS_REQUEST AS A
        JOIN YBC_SWS_APPROVER AS B
          ON A~REQUESTID = B~REQUESTID
       WHERE B~APPROVERID = @GS_RESULT-USERID
         AND A~STATUS IN ('0', '1')  "已提交 处理中
         INTO TABLE @DATA(LT_REQUEST).

      IF SY-SUBRC = 0.
        SELECT *
          INTO CORRESPONDING FIELDS OF TABLE LT_APPROVER
          FROM YBC_SWS_USER
         WHERE USERID = GS_RESULT-AG_USERID.


        SORT LT_REQUEST BY REQUESTID.
        DELETE ADJACENT DUPLICATES FROM LT_REQUEST COMPARING REQUESTID.

        LOOP AT LT_REQUEST INTO DATA(LS_REQUEST).
          CALL FUNCTION 'YBC_SWS_SEND_NEXT'
            EXPORTING
              IV_REQUESTID = LS_REQUEST-REQUESTID
*       IMPORTING
*             ES_MSG       =
            TABLES
              IT_APPROVER  = LT_APPROVER.
        ENDLOOP.
      ENDIF.

    ENDIF.

    PERFORM FORM_GET_DATA.
    LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
    LR_COLUMNS->SET_OPTIMIZE( 'X' ).
    LS_SOLID = 'XX'.
    GR_TABLE->REFRESH( EXPORTING S_STABLE = LS_SOLID ).

    LEAVE TO SCREEN 0.
  ENDIF.
ENDFORM.
